package com.cheung.leetcode.arr;

import  org.junit.jupiter.api.Test;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.arr
 * @Author :  cheungming
 * @CreateTime : 2024-07-17 09:05:27
 * @Description : 121 买卖股票的最佳时机
 * @Version : 1.0
 */
public class Code121Test {

    public int maxProfit(int[] prices) {
        // 想复杂了，这题是只买卖一次
        int cost = Integer.MAX_VALUE, profit = 0;
        for (int price : prices) {
            cost = Math.min(cost, price);
            profit = Math.max(profit, price - cost);
        }
        return profit;
    }

    @Test
    public void test1() {
        int[] prices = {7, 1, 5, 3, 6, 4};
        assert maxProfit(prices) == 5;
    }

    @Test
    public void test2() {
        int[] prices = {7, 6, 4, 3, 1};
        assert maxProfit(prices) == 0;
    }
}
